package com.clp.protocol.iec104.apdu.asdu.info_obj.info_elem;

import com.clp.protocol.core.utils.ByteUtil;
import com.clp.protocol.iec104.definition.Tm;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 设定值命令，短浮点数
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class C_SE_NC_1_InfoElem extends TaInfoElem {
    /**
     * 短浮点数用float表示
     */
    private float floatVal;

    @Override
    public InfoElem initBy(ByteBuf buf) {
        byte[] bytes = new byte[typeTag().getInfoElemBytesLen()];
        buf.readBytes(bytes);
        this.floatVal = ByteUtil.bytes4ToFloatLE(bytes);
        return this;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void writeTo(ByteBuf buf) {
        buf.writeFloatLE(floatVal);
    }

    @Override
    public Tm.ValType getValType() {
        return Tm.ValType.FLOAT;
    }

    @Override
    public Number getNumberVal() {
        return floatVal;
    }

    @Override
    public String toString() {
        return "设定值，短浮点数：" + floatVal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        C_SE_NC_1_InfoElem that = (C_SE_NC_1_InfoElem) o;
        return Float.compare(that.floatVal, floatVal) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(floatVal);
    }
}
